package com.xcm.util;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * 多媒体工具类
 *
 * @author dxy
 */
public class MultimediaUtils {
	/**
	 * 读取视频时长
	 *
	 * @param videoFullPath 视频完整路径
	 * @return double(时长)
	 */
	public static double readVideoDuration(String videoFullPath) {
		double videoDuration = 0.0;
		if (StringUtils.isBlank(videoFullPath)) {
			throw new NullPointerException("视频保存路径不能为空");
		}
		File videoFile = new File(videoFullPath);
		Encoder encoder = new Encoder();
		try {
			MultimediaInfo info = encoder.getInfo(videoFile);
			long duration = info.getDuration();
			double doubleDuration = (double) duration;
			//精确到秒
			videoDuration = doubleDuration / 1000;
		} catch (EncoderException e) {
			e.printStackTrace();
		}
		return videoDuration;
	}

	/**
	 * 读取视频时长
	 *
	 * @param videoFile File
	 * @return double(时长)
	 */
	public static double readVideoDuration(File videoFile) {
		double videoDuration = 0.0;
		if (videoFile == null) {
			throw new NullPointerException("视频文件不能为空");
		}
		Encoder encoder = new Encoder();
		try {
			MultimediaInfo info = encoder.getInfo(videoFile);
			long duration = info.getDuration();
			double doubleDuration = (double) duration;
			//精确到秒
			videoDuration = doubleDuration / 1000;
		} catch (EncoderException e) {
			e.printStackTrace();
		}
		return videoDuration;
	}

	/**
	 * 获取视频的毫秒值
	 *
	 * @param videoFile File
	 * @return long(视频时长)
	 */
	public static Long readVideoLongDuration(File videoFile) {
		if (videoFile == null) {
			throw new NullPointerException("视频文件不能为空");
		}
		Encoder encoder = new Encoder();
		try {
			MultimediaInfo info = encoder.getInfo(videoFile);
			return info.getDuration();
		} catch (EncoderException e) {
			e.printStackTrace();
		}
		return null;
	}
}
